<?php

namespace app\controller\admin\mini;


use app\model\api\Mini;
use app\model\api\MiniAttire;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Good
 * @package app\controller\admin\mini
 */
class Attire extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法
    public $orderRule = ['id' => 'desc'];

    public function _initialize()
    {
        $this->model = new MiniAttire();
    }


    public function index()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->where($where)->field(
                'record_id as id,about,attire_effect,change_img,get_way,if_change,img,series,show_img,unlock'
            )->order($order)->cache(true)->select()->toArray();
        } else {
            $limit = $this->request->param('limit', 10);
            $data  = $this->model->alias('a')->join('mini_record m','a.record_id=m.id')->field(
                'record_id as id,about,attire_effect,change_img,get_way,if_change,img,series,show_img,unlock,m.name'
            )->where($where)->order($order)->paginate($limit)->toArray();
        }
        return $this->success('数据获取成功', $data);
    }

    //添加
    public function add()
    {
        Db::startTrans();
        $post=CommonFun::filterPostData($this->request->post());

        $record_insData['primary_type']='装扮';
        $record_insData['second_type']=!empty($post['second_type'])?$post['second_type']:'';
        $record_insData['name']=!empty($post['name'])?$post['name']:'';
        $record_insData['img_url']=!empty($post['img'])?$post['img']:'';
        $record_insData['if_show']=1;
        $save_record=(new Mini())->insert($record_insData);
        $record_id = (new Mini())->getLastInsID();
        if (!$save_record) throw new \Exception('保存基本信息失败');
        $insData['img'] = !empty($post['img'])?$post['img']:'';
        $insData['show_img'] = !empty($post['show_img'])?$post['show_img']:'';
        $insData['change_img'] = !empty($post['change_img'])?$post['change_img']:'';
        $insData['record_id']  = $record_id;
        $insData['about'] = !empty($post['about'])?$post['about']:'';
        $insData['series'] = !empty($post['series'])?$post['series']:'';
        $insData['unlock'] = !empty($post['unlock'])?$post['unlock']:'';
        $attire_effect=[];
        $post['attire_effect_title'] = explode(',',!empty($post['attire_effect_title'])?$post['attire_effect_title']:'');
        $post['attire_effect_content'] = explode(',',!empty($post['attire_effect_content'])?$post['attire_effect_content']:'');
        foreach ($post['attire_effect_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $attire_effect[] =array(
                'icon'=>'',
                'name'=>$k,
                'effect'=>$post['attire_effect_content'][$i]
            );
        }
        $insData['attire_effect']=json_encode($attire_effect,JSON_UNESCAPED_UNICODE);
        $get_way=[];
        $post['get_way_title'] = explode(',',!empty($post['get_way_title'])?$post['get_way_title']:'');
        $post['get_way_content'] = explode(',',!empty($post['get_way_content'])?$post['get_way_content']:'');
        foreach ($post['get_way_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $get_way[] =array(
                'name'=>$k,
                'effect'=>$post['get_way_content'][$i]
            );
        }
        $insData['get_way']=json_encode($get_way,JSON_UNESCAPED_UNICODE);

        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            Db::commit();
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }

    //编辑
    public function edit()
    {
        Db::startTrans();
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        $record=(new Mini())->find($row['record_id']);
        empty($row) && $this->error('数据不存在');
        empty($record) && $this->error('数据不存在');
        $record->name = !empty($post['name'])?$post['name']:'';
        $record->second_type=!empty($post['second_type'])?$post['second_type']:'';
        $row->img  = !empty($post['img'])?$post['img']:'';
        $row->show_img  = !empty($post['show_img'])?$post['show_img']:'';
        $row->change_img  = !empty($post['change_img'])?$post['change_img']:'';
        $row->about = !empty($post['about'])?$post['about']:'';
        $row->series = !empty($post['series'])?$post['series']:'';
        $row->unlock = !empty($post['unlock'])?$post['unlock']:'';
        $attire_effect=[];
        $post['attire_effect_title'] = explode(',',!empty($post['attire_effect_title'])?$post['attire_effect_title']:'');
        $post['attire_effect_content'] = explode(',',!empty($post['attire_effect_content'])?$post['attire_effect_content']:'');
        foreach ($post['attire_effect_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $attire_effect[] =array(
                'icon'=>'',
                'name'=>$k,
                'effect'=>$post['attire_effect_content'][$i]
            );
        }
        $row->attire_effect=json_encode($attire_effect,JSON_UNESCAPED_UNICODE);
        $get_way=[];
        $post['get_way_title'] = explode(',',!empty($post['get_way_title'])?$post['get_way_title']:'');
        $post['get_way_content'] = explode(',',!empty($post['get_way_content'])?$post['get_way_content']:'');
        foreach ($post['get_way_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $get_way[] =array(
                'name'=>$k,
                'effect'=>$post['get_way_content'][$i]
            );
        }
        $row->get_way=json_encode($get_way,JSON_UNESCAPED_UNICODE);
        try {
            $save = $row->save();
            $record=$record->save();
            if ($save === false && $record === false) throw new \Exception('保存失败');
            Db::commit();
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }

    public function info(){
        $id = $this->request->param('id');
        $attire = $this->model->where('record_id',$id)->find()->toArray();
        $record_info  = (new Mini())->detail($id);
        $attire['name'] = !empty($record_info['name'])?$record_info['name']:'';
        $attire['second_type'] = !empty($record_info['second_type'])?$record_info['second_type']:'';
        $attire['img']  = !empty($attire['img'])?$attire['img']:'';
        $attire['show_img']  = !empty($attire['show_img'])?$attire['show_img']:'';
        $attire['change_img']  = !empty($attire['change_img'])?$attire['change_img']:'';
        $attire['about']  = !empty($attire['about'])?$attire['about']:'';
        $attire['series']  = !empty($attire['series'])?$attire['series']:'';
        $attire['unlock']  = !empty($attire['unlock'])?$attire['unlock']:'';
        $attire['attire_effect']  = !empty(json_decode($attire['attire_effect']))?json_decode($attire['attire_effect']):'';
        $attire['get_way']  = !empty(json_decode($attire['get_way']))?json_decode($attire['get_way']):'';
        return $this->success('获取成功', $attire);
    }


    //删除
    public function del()
    {
        Db::startTrans();
        $ids = array_filter($this->request->post('ids'));

        if (!$ids) {
            return $this->error('参数ids不能为空');
        }
        try{
            if ($this->model->where('record_id','in',$ids)->delete() && (new Mini())->where('id','in',$ids)->delete()) {
                Db::commit();
                $this->clearCache();
                return $this->success('数据删除成功');
            } else {
                return $this->error('数据删除失败');
            }
        }catch (\Exception $e){
            Db::rollback();
            return $this->exceptionError($e);
        }
    }
}